#include "Skin.h"

Skin::Skin(QString name, QString extension, QString path) {
	this->name = name;
	this->extension = extension;
	this->path = path;
}


/**
 * Create a skin instance from a path (must have at least a valid info.ini file)
 */
Skin* Skin::fromPath(QString path) {
	
	// Checking the ini file for Name and Extension
	QSettings settings(QDir::cleanPath(path + "/" + Properties::SKINSNFO), QSettings::IniFormat);
	
	QString name = settings.value("name",tr("Default skin")).toString();
	QString extension = settings.value("extension",Properties::DEFAULT_EXTENSION).toString();
	
	return new Skin(name, extension, path);
}


/**
 * Create the default skin instance.
 */
Skin* Skin::fromDefault() {
	return new Skin(tr("Default skin"), Properties::DEFAULT_EXTENSION, Properties::DEFAULT_CARDPATH);
}




/**
 * Gather a list of skins in the SKINSPATH dir.
 * The output is a hashmap containing dirpath-skinname pair.
 * (the former is used for path to skin content, the later for actual name)
 */
QHash<QString,QString> Skin::skins() {
	
	QHash<QString,QString> skinNames;
	
	
	/* Looking for Skin directories... */
	QDir skinsDir(Properties::SKINSPATH);
	
	skinsDir.setFilter(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
	skinsDir.setSorting(QDir::Name);
	
	
	QStringList entryList = skinsDir.entryList();
	
	
	/* For each directory, looking for the name in the info.ini */
	foreach(QString dir, entryList) {
		
		QString dirPath = Properties::SKINSPATH + "/" + dir;
		QString nfoPath = dirPath + "/" + Properties::SKINSNFO;
		QSettings settings(QDir::cleanPath(nfoPath), QSettings::IniFormat);
		
		// We say skin exists if there is a non-empty name present in the ini
		QString name = settings.value("name","").toString();
		
		// If the skin exists, adding it
		if (!name.isEmpty()) skinNames[dirPath] = name;
	}
	
	return skinNames;
}




QString Skin::imageUrlNormal(QString basename) {
	return imageUrl(basename);
}

QString Skin::imageUrlThumb(QString basename) {
	return imageUrl(basename, Properties::CARD_APPEND);
}

QString Skin::imageUrlCardBackNormal() {
	return imageUrlNormal(Properties::FILENAME_CardBack);
}
QString Skin::imageUrlCardBackThumb() {
	return imageUrlThumb(Properties::FILENAME_CardBack);
}

QString Skin::imageUrlCoupFourre() {
	return imageUrlNormal(Properties::FILENAME_CoupFourre);
}





/**
 * According to a card basename (e.g. hazard_outofgas), will calculate the whole path to the card picture:
 * the skin path (or default skin internal path) + basename + extension
 * 
 * The append parameter allow to have the normal or thumbnailed image.
 */
QString Skin::imageUrl(QString basename, QString append) {
	
	QString defaultURL = QDir::cleanPath(Properties::DEFAULT_CARDPATH + "/" + basename + append + "." + Properties::DEFAULT_EXTENSION);
	QString skinURL = QDir::cleanPath(path + "/" + basename + append + "." + extension);
	
	
	// Setting the path to default or skin path (if available)
	
	QFileInfo skinNfo(skinURL);
	if (skinNfo.exists() && skinNfo.isReadable())
		return skinURL;
	
	return defaultURL;
}



QString Skin::getName() {
	return name;
}
